package com.wpw.dream.spider.processor;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import com.wpw.dream.spider.constant.Constants;

import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.scheduler.DuplicateRemovedScheduler;
import us.codecraft.webmagic.scheduler.MonitorableScheduler;

public class SpiderQueueScheduler extends DuplicateRemovedScheduler implements MonitorableScheduler {

	private BlockingQueue<Request> queue = new LinkedBlockingQueue<Request>();
	
	private Long maxArticleTime;

	public SpiderQueueScheduler(Long maxArticleTime) {
		this.maxArticleTime = maxArticleTime;
	}
	
    @Override
    public void pushWhenNoDuplicate(Request request, Task task) {
        queue.add(request);
    }

    @Override
    public synchronized Request poll(Task task) {
        return queue.poll();
    }

    @Override
    public int getLeftRequestsCount(Task task) {
        return queue.size();
    }

    @Override
    public int getTotalRequestsCount(Task task) {
        return getDuplicateRemover().getTotalRequestsCount(task);
    }

	@Override
	public synchronized void push(Request request, Task task) {
		Spider spider = (Spider)task;
		System.out.println("=====schelduer中要push：" + request.getUrl());
		//文章时间
		Object extra = request.getExtra(Constants.ARTICLE_TIME);
		if (this.maxArticleTime != null) {
			if (extra != null) {
				Long articleTime = Long.parseLong(extra.toString());
				if (this.maxArticleTime >= articleTime) {
					spider.stop();
				}
			} else {
				super.push(request, task);
			}
		} else {
			super.push(request, task);
		}
		
	}

}
